package com.zswl.decorate.base;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zswl.decorate.R;
import com.zswl.decorate.util.RxUtil;
import com.zswl.decorate.widget.SwipeRecyclerView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

public abstract class BaseListFragment<B extends BaseBean, A extends BaseRecycleViewAdapter<B>> extends BaseFragment implements SwipeRecyclerView.OnLoadListener {
    @BindView(R.id.rv)
    protected SwipeRecyclerView recyclerView;
    protected List<B> data;
    protected A adapter;
    protected int page;
    protected int limit = 10;
    protected BaseObserver<List<B>> observer;//加载数据回调

    @Override
    protected void init(View view) {
        data = new ArrayList<>();
        initAdapter();
        recyclerView.getRecyclerView().setLayoutManager(getLayoutManager() == null ? new LinearLayoutManager(context) : getLayoutManager());
        recyclerView.setAdapter(adapter);
        recyclerView.setOnLoadListener(this);
        recyclerView.getSwipeRefreshLayout().setColorSchemeResources(R.color.colorPrimary);
        observer = new BaseObserver<List<B>>(context) {
            @Override
            public void receiveResult(List<B> result) {
                recyclerView.setRefreshing(false);
                data.addAll(result);
                adapter.notifyDataSetChanged();
                if (result.size() == 0) {
                    recyclerView.onNoMore("--- 到底了---");
                } else {
                    recyclerView.stopLoadingMore();
                }

            }
        };
        getListData(page);
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        Type type = getClass().getGenericSuperclass();
        Class<A> clazz = (Class<A>) ((ParameterizedType) type).getActualTypeArguments()[1];
        try {
            Constructor constructor = clazz.getDeclaredConstructor(Context.class, List.class, int.class);
            adapter = (A) constructor.newInstance(context, data, getItemLayout());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Fragment.InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }

    }

    /**
     * 列表布局
     *
     * @return
     */
    protected abstract int getItemLayout();


    /**
     * 获取列表数据
     *
     * @param page
     */
    protected void getListData(int page) {
        if (getApi(page) == null) {
            recyclerView.setRefreshing(false);
            recyclerView.onNoMore("");
            return;
        }
        getApi(page).compose(RxUtil.io_main(lifeSubject))
                .subscribe(observer);
    }

    /**
     * 获取api接口
     *
     * @return
     */

    protected abstract Observable<HttpResult<List<B>>> getApi(int page);

    /**
     * 列表布局管理器
     *
     * @return
     */
    protected abstract RecyclerView.LayoutManager getLayoutManager();


    @Override
    public void onRefresh() {
        data.clear();
        page = 0;
        getListData(page);
    }

    @Override
    public void onLoadMore() {
        page = page + limit;
        getListData(page);
    }


}
